package com.hotSpot.chapter.two.four;


/**
 * 
 * @author wangjunjie
 * VM arguments参数配置:
 * 
 *  基于jdk8运行时，配置-Xms5M -Xmx10M -Xss2M -XX:+HeapDumpOnOutOfMemoryError，结果导致电脑卡死，
 *  后面加入参数-XX:MaxMetaspaceSize=2M后控制台可以正常打印出异常,
 *  参数配置:-Xms5M -Xmx10M -Xss2M -XX:MaxMetaspaceSize=2M -XX:+HeapDumpOnOutOfMemoryError
 *  java.lang.OutOfMemoryError: Metaspace
	Dumping heap to java_pid1042.hprof ...
	Heap dump file created [578267 bytes in 0.050 secs]
	Error occurred during initialization of VM
	java.lang.OutOfMemoryError: Metaspace
	<<no stack trace available>>
 *  
 *  基于jdk1.7运行时，配置参数如下：
 *  -Xms5M -Xmx10M -Xss1M -XX:PermSize=2M -XX:MaxPermSize=5M -XX:+HeapDumpOnOutOfMemoryError
 *  控制台出现异常后程序还在继续运行，控制台红色按钮一直亮着，当然内存也没有dump出来
 */
public class JavaVMStackOOM {
    private void dontStop(){
    	while(true){
    		
    	}
    }
    
    public void stackLeakByThread(){
		while(true){
			
			Thread thread = new Thread(new Runnable(){
				@Override
				public void run(){
					dontStop();
				}
			});
			thread.start();
    	}
	}
	 
    public static void main(String[] args){
    	JavaVMStackOOM oom = new JavaVMStackOOM();
		oom.stackLeakByThread();
    }
    
}
